## Clear environment
rm(list=ls(all=TRUE))
## Set working directory
setwd("~/Documents/GitHub/prop18/data-challenge/02-clean-data")
## Load packages
library(tidyverse)
library(leaflet)
library(tigris)
## Import data
youth_vote <- read.csv("clean_youth_vote_200928.csv")
tigris package (includes pre-loaded map of U.S.) and leaflet##
|
| | 0%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 10%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========= | 14%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 46%
|
|================================= | 47%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|=================================== | 51%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|========================================== | 61%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 70%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
## Split dataset by year
youth_vote_16 <- filter(youth_vote, year == 2016)
youth_vote_18 <- filter(youth_vote, year == 2018)
youth_vote_16 <- geo_join(states, youth_vote_16, "STUSPS", "state_code")
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
youth_vote_18 <- geo_join(states, youth_vote_18, "STUSPS", "state_code")
youth_vote_16$primary_17 <- ifelse(youth_vote_16$primary_17 %in% "Y", "Yes", "No")
youth_vote_16$primary_17 <- as.factor(youth_vote_16$primary_17)
youth_vote_18$primary_17 <- ifelse(youth_vote_18$primary_17 %in% "Y", "Yes", "No")
youth_vote_18$primary_17 <- as.factor(youth_vote_18$primary_17)
pal_turnout <- colorFactor("Purples", domain = youth_vote_16$primary_17)
pal_register <- colorFactor("Oranges", domain = youth_vote_18$primary_17)
popup_turnout_16 <- paste0(youth_vote_16$state, "<br>Youth Turnout (2016): ",
as.character(youth_vote_16$turnout * 100), "%")
popup_turnout_18 <- paste0(youth_vote_18$state, "<br>Youth Turnout (2018): ",
as.character(youth_vote_18$turnout * 100), "%")
popup_register_16 <- paste0(youth_vote_16$state, "<br>Youth Registration (2016): ",
as.character(youth_vote_16$register * 100), "%")
popup_register_18 <- paste0(youth_vote_18$state, "<br>Youth Registration (2018): ",
as.character(youth_vote_18$register * 100), "%")
# Generate map for 2016
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-98.483330, 38.712046, zoom = 4) %>%
addPolygons(data = youth_vote_16,
fillColor = ~pal_turnout(youth_vote_16$primary_17),
fillOpacity = 0.7,
weight = 0.2,
smoothFactor = 0.2,
popup = ~popup_turnout_16) %>%
addLegend(pal = pal_turnout,
values = youth_vote_16$primary_17,
position = "bottomright",
title = "States with Primary Voting<br>for 17 year-olds")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
# Generate map for 2018
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-98.483330, 38.712046, zoom = 4) %>%
addPolygons(data = youth_vote_18,
fillColor = ~pal_turnout(youth_vote_18$primary_17),
fillOpacity = 0.7,
weight = 0.2,
smoothFactor = 0.2,
popup = ~popup_turnout_18) %>%
addLegend(pal = pal_turnout,
values = youth_vote_18$primary_17,
position = "bottomright",
title = "States with Primary Voting<br>for 17 year-olds")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
# Generate map for 2016
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-98.483330, 38.712046, zoom = 4) %>%
addPolygons(data = youth_vote_16,
fillColor = ~pal_register(youth_vote_16$primary_17),
fillOpacity = 0.7,
weight = 0.2,
smoothFactor = 0.2,
popup = ~popup_register_16) %>%
addLegend(pal = pal_register,
values = youth_vote_16$primary_17,
position = "bottomright",
title = "States with Primary Voting<br>for 17 year-olds")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
# Generate map for 2018
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-98.483330, 38.712046, zoom = 4) %>%
addPolygons(data = youth_vote_18,
fillColor = ~pal_register(youth_vote_18$primary_17),
fillOpacity = 0.7,
weight = 0.2,
smoothFactor = 0.2,
popup = ~popup_register_18) %>%
addLegend(pal = pal_register,
values = youth_vote_18$primary_17,
position = "bottomright",
title = "States with Primary Voting<br>for 17 year-olds")
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'